<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>统计字符串中各字符在字符串中出现的数量</title>
    <!--好的代码像粥一样,是得用时间和精力熬的。-->
</head>
<body>
<h3>
    输入'abcd';
    输出 a:1,b:1,c:1,d:1
</h3>
<script>
    var s='jhdoiweesdds';
    var n=(s.split('e')).length-1;
    document.write(s+"<br/>e:"+n);

    var str = "jhdoiweesdds",json = {};
    for (var i = 0, l = str.length; i < l; i++) {
        json[str[i]] = (json[str[i]] + 1) || 1;
    }
    console.log(JSON.stringify(json));
    //-> {"s":2,"d":1,"w":2,"p":1,"r":1,"q":1,"j":1,"x":1,"g":1}

    var testStr = 'jhdoiweesdds';
    var tempObj = {};
    for (var i = 0; i < testStr.length; i++) {
        var char = testStr.charAt(i);//相当于挨个遍历字符串字符，将字符作为key
        console.log(char);
        if (tempObj[char]) {
            tempObj[char]++;
        } else {
            tempObj[char] = 1;
        }
    }
    console.log(tempObj);
</script>
</body>
</html>
